package com.yimi.yinhepda.db.dao;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.OffLinePlatformNoTable;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OffLinePlatformNoTableDao extends BaseDao<OffLinePlatformNoTable, Long> {

    public OffLinePlatformNoTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), OffLinePlatformNoTable.class);
    }

    public void createOrUpdate(String deptCode, List<String> lists) {
        try {
            //删除数据
            List<OffLinePlatformNoTable> tableList = getDataByDeptCode(deptCode);
            if (tableList != null) {
                delete(tableList);
            }
            //插入数据
            if (lists != null) {
                List<OffLinePlatformNoTable> platformNoTables = new ArrayList<>();
                for (String platform : lists) {
                    OffLinePlatformNoTable platformNoTable = new OffLinePlatformNoTable();
                    platformNoTable.setDeptCode(deptCode);
                    platformNoTable.setPlatformNo(platform);
                    platformNoTables.add(platformNoTable);
                }
                insert(platformNoTables);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据部门编码获取数据
     *
     * @param deptCode 部门编码
     * @return
     */
    public List<OffLinePlatformNoTable> getDataByDeptCode(String deptCode) {
        try {
            QueryBuilder<OffLinePlatformNoTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("deptCode", deptCode);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
